package com.petshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.petshop.dto.CartItemRequest;
import com.petshop.dto.CartItemUpdateDTO;
import com.petshop.mapper.CartItemsMapper;
import com.petshop.mapper.ProductsMapper;
import com.petshop.pojo.CartItems;
import com.petshop.pojo.Products;
import com.petshop.service.CartItemsService;
import com.petshop.utils.JwtHelper;
import com.petshop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 20289
 * @description 针对表【cart_items(存储用户购物车中的商品信息)】的数据库操作Service实现
 * @createDate 2025-03-09 22:31:47
 */
@Service
public class CartItemsServiceImpl extends ServiceImpl<CartItemsMapper, CartItems>
        implements CartItemsService {

    @Autowired
    private CartItemsMapper cartItemsMapper;

    @Autowired
    private ProductsMapper productsMapper;

    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 获取购物车的所有商品
     *
     * @return
     */
    @Override
    public Result getAllItem(String token) {
        Long userId = jwtHelper.getUserId(token);
        LambdaQueryWrapper<CartItems> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CartItems::getUserId, userId);
        return Result.ok(cartItemsMapper.selectList(queryWrapper));
    }

    /**
     * 添加一个商品到购物车
     *
     * @param
     * @return
     */
    @Override
    public Result addItem(CartItemRequest cartItemRequest, String token) {
        //获取用户的id
        int userId = jwtHelper.getUserId(token).intValue();
        //根据id查询该商品信息
        LambdaQueryWrapper<Products> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Products::getProductId, cartItemRequest.getProductId());
        CartItems cartItems = new CartItems();
        Products products = productsMapper.selectOne(lambdaQueryWrapper);
        //如果数量大于库存量 添加失败
        if (cartItemRequest.getCount() > products.getStock()) {
            return Result.error("加入失败，库存不足！");
        }
        //查询购物车中是否已经有该用户添加的该商品记录
        LambdaQueryWrapper<CartItems> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(CartItems::getProductId, cartItemRequest.getProductId()).eq(CartItems::getUserId, userId);
        Long l = cartItemsMapper.selectCount(lambdaQueryWrapper1);
        if (l > 0) {
            //增加购物车中该商品数量即可 无需再添加一条记录
            cartItemsMapper.addStock(cartItemRequest.getProductId(), cartItemRequest.getCount());
            return Result.ok("加入购物车成功！");
        } else {
            cartItems.setProductId(cartItemRequest.getProductId());
            cartItems.setUserId(userId);
            cartItems.setProductName(products.getName());
            cartItems.setProductCategory(products.getCategory());
            cartItems.setQuantity(cartItemRequest.getCount());
            cartItems.setProductPrice(products.getPrice());
            cartItems.setCartImage(products.getImageUrl());
            //如果加入购物车的商品数大于库存，则添加到购物车
            cartItemsMapper.insert(cartItems);
            return Result.ok("加入购物车成功！");
        }
    }

    /**
     * 根据购物车记录id从购物车删除多个商品
     *
     * @param cartIds
     * @return
     */
    @Override
    public Result outItem(List<Integer> cartIds) {
        int i = cartItemsMapper.deleteBatchIds(cartIds);
        if (i > 0) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * 修改购物车商品数量
     *
     * @param cartItemUpdateDTO
     * @return
     */
    @Override
    public Result updateQuantity(CartItemUpdateDTO cartItemUpdateDTO) {
        cartItemsMapper.updateQuantity(cartItemUpdateDTO.getCartId(), cartItemUpdateDTO.getQuantity());
        return Result.ok(null);
    }

    /**
     * 根据id从购物车删除1个商品
     *
     * @param cartId
     * @return
     */
    @Override
    public Result outOneItem(Integer cartId) {
        LambdaQueryWrapper<CartItems> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CartItems::getCartId, cartId);
        int i = cartItemsMapper.deleteById(cartId);
        if (i > 0) {
            return Result.ok("删除成功");
        }
        return Result.error("商品已经删除，请勿重复操作！");
    }

    /**
     * 根据id数组查询购物车商品信息
     *
     * @param cartIds
     * @return
     */
    @Override
    public List<CartItems> selectCartItems(ArrayList<Integer> cartIds) {
        LambdaQueryWrapper<CartItems> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(CartItems::getCartId, cartIds);
        return cartItemsMapper.selectList(lambdaQueryWrapper);
    }
}




